<template>
  <div class="appify">
    <el-form
      :model="dynamicValidateForm"
      ref="dynamicValidateForm"
      label-width="100px"
      class="demo-dynamic"
    >
      <el-form-item prop="email" label="用户名">
        <el-input v-model="dynamicValidateForm.email"></el-input>
      </el-form-item>
      <el-form-item
        v-for="domain in dynamicValidateForm.domains"
        label="密码"
        :key="domain.key"
        prop="password"
      >
        <el-input v-model="domain.value" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="Login()">登录</el-button>
        <el-button @click="reg()">注册</el-button>
        <el-button @click="del()">注销用户</el-button>
        <el-button @click="goUpdata()">修改密码</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dynamicValidateForm: {
        domains: [
          {
            value: "",
          },
        ],
        email: "",
      },
    };
  },
  methods: {
    goUpdata(){
      this.$router.push('/updata')
    },
    del(){
      let name = this.dynamicValidateForm.email;
      let pwd = this.dynamicValidateForm.domains[0].value;
      this.$axios.post("/api/del",{ name, pwd }).then((res)=>{
        console.log(res);
      })
    },
    async initData(name, pwd) {
      let data = await this.$axios.post("/api/login", { name, pwd });
      if (data.data.code === 2) {
        alert(data.data.msg);
        localStorage.setItem("token", data.data.token);
        this.$router.push("/home");
      } else {
        alert(data.data.msg);
      }
      console.log(data);
    },
    async register(name,pwd) {
      const res = await this.$axios.post("/api/register",{ name, pwd })
      if(res.data.code==2){
        alert(res.data.msg);
      }else{
        alert(res.data.msg);
      }
      console.log(res);
    },
    reg() {
      let user = this.dynamicValidateForm.email;
      let psw = this.dynamicValidateForm.domains[0].value;
      this.register(user,psw)
    },
    Login() {
      let user = this.dynamicValidateForm.email;
      let psw = this.dynamicValidateForm.domains[0].value;
      this.initData(user, psw);
    },
  },
};
</script>

<style lang="scss" scoped>
.appify {
  width: 500px;
  margin: 100px auto;
}
</style>